<template>
    <view class="partner-add pd-v-24 pd-b-72">
      
        <view class="partner-add__body overflow-h">
            <u--form
                labelWidth="80"
                labelPosition="left"
                :model="form"
                :rules="rules"
                ref="uForm"
            >
                <view class="border8 pd-l-24 bg-white mr-t-20">
                    <u-form-item
                        label="合作伙伴名称"
                        prop="nickName"
                        borderBottom
                    >
                        <u--input
                            v-model.trim="form.nickName"
                            border="none"
                            :customStyle="{padding:'12rpx 0'}"
                            placeholder="请输入合作伙伴名称"
                        ></u--input>
                    </u-form-item>
                    <u-form-item
                        label="登录账号"
                        prop="userName"
                        borderBottom
                    >
                        <u--input
                            v-model.trim="form.userName"
                            border="none"
                            :disabled="props.id != ''"
                            :customStyle="{padding:'12rpx 0'}"
                            placeholder="请输入登录账号"
                        ></u--input>
                    </u-form-item>
                    <u-form-item
                        label="手机号"
                        prop="phonenumber"
                        borderBottom
                    >
                        <u--input
                            v-model.trim="form.phonenumber"
                            border="none"
                            :customStyle="{padding:'12rpx 0'}"
                            placeholder="请输入手机号"
                        ></u--input>
                    </u-form-item>
                    <u-form-item
                        label="分佣比例"
                        prop="sharingRatio"
                        borderBottom
                    >
                        
                        <u--input
                            v-model.trim="form.sharingRatio"
                            border="none"
                            :customStyle="{padding:'12rpx 12rpx 12rpx 0'}"
                            placeholder="请输入分佣比例"
                            :suffixIconStyle="{fontSize:'22rpx'}" suffixIcon="%"
                        ></u--input>
                    </u-form-item>
                </view>

                <view class="border8 pd-l-24 bg-white mr-t-20">
                    <u-form-item
                        label="账号状态"
                        borderBottom
                        prop="status"
                    >
                        
                        <u-radio-group
                            v-model="form.status"
                        >
                            <u-radio
                                :label="'开启'"
                                name="0"
                            >
                            </u-radio>
                            <u-radio
                                :label="'关闭'"
                                name="1"
                            >
                            </u-radio>
                        </u-radio-group>
                    </u-form-item>
                    <u-form-item
                        label="提现"
                        borderBottom
                        prop="isWithdrawal"
                    >
                        
                        <u-radio-group
                            v-model="form.isWithdrawal"
                        >
                            <u-radio
                                :label="'开启'"
                                name="1"
                            >
                            </u-radio>
                            <u-radio
                                :label="'关闭'"
                                name="0"
                            >
                            </u-radio>
                        </u-radio-group>
                    </u-form-item>
                </view>


                <view class="mr-t-40 flex flex-center flex-j-center">
                    <u-button shape="circle"  :customStyle="{height:'88rpx',width:'600rpx'}" color="#35BE65"  text="确认" @click="submit"></u-button>
                </view>
                  
            </u--form>
        </view>
          

        <view class="partner-add__footer">
            
              
              
        </view>
          
          
    </view>
</template>

<script setup>
import { useStore } from '@/store/index.js';
import { ref, reactive, onMounted, nextTick,defineProps } from 'vue';
import { onLoad, onReady, onShow } from '@dcloudio/uni-app';


import { cooperativePartnerQueryById, cooperativePartnerAdd, cooperativePartnerUpdate  } from '@/api/proxy.js'

const props = defineProps({
    id: {
        type: String,
        default: ''
    }
})

const store = useStore();
const form = ref({
    nickName: undefined,
    userName: undefined,
    password:'123456',
    phonenumber:undefined,
    roleIds:[100],
    status:undefined,
    isWithdrawal:undefined,
    sharingRatio:undefined,
    operationPage:'2',
    batchNumber:undefined
});
const uForm = ref(null);
const rules = ref({
    nickName: [
        {
            required: true,
            message: '请输入商户名称',
            trigger: ['blur','change']
        }
    ],
    userName: [
        {
            required: true,
            message: '请输入登录账号',
            trigger: ['blur','change']
        }
    ],
    phonenumber: [
        {
            message: '请输入手机号',
            trigger: ['blur','change'],
            validator: (rule, value, callback) => {
                if (!value) {
                    callback('请输入手机号');
                    return false;
                } else if (!/^1[3-9]\d{9}$/.test(value)) {
                    callback('请输入正确的手机号');
                    return false;
                } else {
                    callback();
                }
            }
        }
    ],
    status: [
        {
            required: true,
            message: '请选择账号状态',
            trigger: ['blur','change']
        }
    ],
    isWithdrawal: [
        {
            required: true,
            message: '请选择提现状态', 
            trigger: ['blur','change']
        }
    ],
    sharingRatio: [
        {   
            trigger: ['blur','change'],
            validator: (rule, value, callback) => {
                if (value) {
                    if(value < 0 || value > 100) {
                        callback('分摊比例必须在0-100之间');
                        return false;
                    }
                }else {
                    callback();
                }
            }
        }
    ]
    
})

const storeList = ref([]);

const allChecked = ref([])
const checkList = ref([]);

onReady(() => {
    setRules()
})

onShow(() => {
    
})

onLoad(() => {
    if(props.id) {
        getCooperativePartnerQueryById()
    } else {
        form.value.batchNumber = generateTimestamp()
    }

})


const generateTimestamp = () => {
    const now = new Date();
    const year = now.getFullYear();
    const month = (now.getMonth() + 1).toString().padStart(2, "0"); // 月份从0开始，所以需要+1
    const day = now.getDate().toString().padStart(2, "0");
    const hours = now.getHours().toString().padStart(2, "0");
    const minutes = now.getMinutes().toString().padStart(2, "0");
    const seconds = now.getSeconds().toString().padStart(2, "0");
    const milliseconds = now.getMilliseconds().toString().padStart(3, "0");

    return `${year}${month}${day}${hours}${minutes}${seconds}${milliseconds}`;
}

const getCooperativePartnerQueryById = () => { 
    cooperativePartnerQueryById({userId:props.id
    }).then(res => {
        if(res.code == 200) {
            res.data.sharingRatio = String(res.data.sharingRatio)
            form.value = {...res.data};
        }
    })
}



const setRules = () => {
    uForm.value.setRules(rules.value);
}


const submit = () => {
    uForm.value.validate().then(res => {
        let params = {...form.value};
        if(props.id) {
            params.id = props.id;
            cooperativePartnerUpdate(params).then(res => { 
                if(res.code == 200) {
                    uni.showToast({
                        title: '修改成功',
                        icon: 'none'
                    })
                    setTimeout(() => {
                        uni.navigateBack({
                            delta: 1
                        })
                    },1500)
                } else {
                    uni.showToast({
                        title: res.msg,
                        icon: 'none'
                    })
                }
            }).catch(err => {
                console.log(err,'eeeeeeeeeeeeeeeee')
            })
        } else {
            
            cooperativePartnerAdd(params).then(res => { 
                if(res.code == 200) {
                    uni.showToast({
                        title: '添加成功',
                        icon: 'none'
                    })
                    setTimeout(() => {
                        uni.navigateBack({
                            delta: 1
                        })
                    },1500)
                } else {
                    uni.showToast({
                        title: res.msg,
                        icon: 'none'
                    })
                }
            })
        }
    }).catch(err => {
        console.log(err,'eeeeeeeeeeeeeeeee')
    })
}

</script>

<style scoped lang="scss">
.partner-add {
    background: #F7F7F7;
    min-height:100vh;
    
    ::v-deep .u-form-item__body__right__message {
        margin-left:0!important;
    }

    .close-btn {
        right:-10rpx;
        top:0;
    }
}
</style>
